package edu.hawaii.myisern.model;

import static org.junit.Assert.assertEquals;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import edu.hawaii.myisern.collaborations.jaxb.CollaboratingOrganizations;
import edu.hawaii.myisern.collaborations.jaxb.Collaboration;
import edu.hawaii.myisern.collaborations.jaxb.CollaborationTypes;
import edu.hawaii.myisern.collaborations.jaxb.OutcomeTypes;
import edu.hawaii.myisern.collaborations.jaxb.Years;
import edu.hawaii.myisern.organizations.jaxb.AffiliatedResearchers;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.organizations.jaxb.ResearchKeywords;
import edu.hawaii.myisern.researchers.jaxb.Researcher;

/**
 * Provides test cases.
 * 
 * @author Jon Lao
 * @author Jared Sunouchi
 * @author Chiao-Fen Zielinski-Liu
 */
public class TestMyIsernModel {

  
  /**
   * Tests the collaboration function for adding under normal situations.
   *
   * @throws Exception If problems occur
   */
  @Test
  public void testMyIsernModel() throws Exception {    
    MyIsernModel myIsernModel = MyIsernModel.getInstance();
    
    //test empty list
    List<String> list = new ArrayList<String>();
    String listString = null;
    assertEquals("test empty list", list, myIsernModel.createList(listString));
    
    list.add("University of Hawaii");
    list.add("University of Maryland");
    
    listString = "University of Hawaii,University of Maryland";
    assertEquals("test non-empty list", list, myIsernModel.createList(listString));
    
    listString = "University of Hawaii, University of Maryland";
    assertEquals("test list string", listString, myIsernModel.createListString(list));
    
    Collaboration newCollaboration = new Collaboration();
    //name
    newCollaboration.setName("UH-UM");
    //collaborating organizations
    CollaboratingOrganizations newCollaboratingOrganizations = new CollaboratingOrganizations();
    newCollaboratingOrganizations.getCollaboratingOrganization().add("University of Hawaii");
    newCollaboratingOrganizations.getCollaboratingOrganization().add("University of Maryland");
    newCollaboration.setCollaboratingOrganizations(newCollaboratingOrganizations);
    //collaboration types
    CollaborationTypes newCollaborationTypes = new CollaborationTypes();
    newCollaborationTypes.getCollaborationType().add("Student Intern");
    newCollaboration.setCollaborationTypes(newCollaborationTypes);
    //years
    Years newYears = new Years();
    BigInteger yearOne = new BigInteger("2007");
    BigInteger yearTwo = new BigInteger("2008");
    newYears.getYear().add(yearOne);
    newYears.getYear().add(yearTwo);
    newCollaboration.setYears(newYears);
    //outcome types
    OutcomeTypes newOutcomeTypes = new OutcomeTypes();
    newOutcomeTypes.getOutcomeType().add("Technical Report");
    newCollaboration.setOutcomeTypes(newOutcomeTypes);
    //description
    newCollaboration.setDescription("Good Job");
    
    int size = myIsernModel.getCollaborations().size();
    myIsernModel.addCollaboration(newCollaboration);
    assertEquals("Check for added collaboration", (size + 1), 
        myIsernModel.getCollaborations().size());
    myIsernModel.editCollaboration(5, newCollaboration);
    size = myIsernModel.getCollaborations().size();
    myIsernModel.removeCollaboration("UH-UM");
    assertEquals("Check for removed collaboration", (size - 1), 
        myIsernModel.getCollaborations().size());
    
    
    Organization newOrganization = new Organization();
    //name
    newOrganization.setName("University of Wisconsin");
    //type
    newOrganization.setType("Academic");
    //contact
    newOrganization.setContact("Chiao-Fen Zielinski");
    //affiliated-researcher
    AffiliatedResearchers newAffiliatedResearchers = new AffiliatedResearchers();
    newAffiliatedResearchers.getAffiliatedResearcher().add("Michal Zielinski");
    newOrganization.setAffiliatedResearchers(newAffiliatedResearchers);
    //country
    newOrganization.setCountry("USA");
    ResearchKeywords newResearchKeywords = new ResearchKeywords();
    newResearchKeywords.getResearchKeyword().add("Software Engineer");
    //description
    newOrganization.setResearchDescription("Great Researcher");
    //homepage
    newOrganization.setHomePage("www2.hawaii.edu/~chiaofen");
    
    size = myIsernModel.getOrganizations().size();
    myIsernModel.addOrganization(newOrganization);
    assertEquals("Check for added organization", (size + 1), 
        myIsernModel.getOrganizations().size());
    myIsernModel.editOrganization(5, newOrganization);
    size = myIsernModel.getOrganizations().size();
    myIsernModel.removeOrganization("University of Wisconsin");
    assertEquals("Check for removed organization", (size - 1), 
        myIsernModel.getOrganizations().size());
    
    Researcher newResearcher = new Researcher();
    newResearcher.setName("Jared Sunouchi");
    newResearcher.setOrganization("University of Hawaii");
    newResearcher.setEmail("jared@hawaii.edu");
    newResearcher.setPictureLink("");
    newResearcher.setBioStatement("Great Programmer");
    
    size = myIsernModel.getResearchers().size();
    myIsernModel.addResearcher(newResearcher);
    assertEquals("Check for added researchers", (size + 1), myIsernModel.getResearchers().size());
    myIsernModel.editResearcher(5, newResearcher);
    size = myIsernModel.getResearchers().size();
    myIsernModel.removeResearcher("Jared Sunouchi");
    assertEquals("Check for removed researcher", (size - 1), myIsernModel.getResearchers().size());
    
    
    // test sort functions
    newCollaboration.setName("A");
    newOrganization.setName("A");
    newResearcher.setName("A");
    myIsernModel.addCollaboration(newCollaboration);
    myIsernModel.addOrganization(newOrganization);
    myIsernModel.addResearcher(newResearcher);
    myIsernModel.sortCollaborations();
    myIsernModel.sortOrganizations();
    myIsernModel.sortResearchers();
    assertEquals("Check that added collaboration is first", newCollaboration.getName(), 
        myIsernModel.getCollaborations().get(0).getName());
    assertEquals("Check that added organization is first", newOrganization.getName(), 
        myIsernModel.getOrganizations().get(0).getName());
    assertEquals("Check that added researcher is first", newResearcher.getName(),
        myIsernModel.getResearchers().get(0).getName());
    myIsernModel.removeCollaboration("A");
    myIsernModel.removeOrganization("A");
    myIsernModel.removeResearcher("A");
    
    while (myIsernModel.getResearchers().contains(newCollaboration)) {
      myIsernModel.removeCollaboration("A");
    }
    while (myIsernModel.getOrganizations().contains(newOrganization)) {
      myIsernModel.removeOrganization("A");
    }
    while (myIsernModel.getResearchers().contains(newResearcher)) {
      myIsernModel.removeResearcher("A");
    }    
  }
}
